#include <paramNormal.h>
#include <crcCalc.h>
#include <flashOpt.h>
#include <hexstr.h>

//================================default normal param begin=============================================
DEV_C51_XDATA_FLAG parmInfoNorStc parmNormIstc;
int parmNormPrint()
{
    logdebug("printf device normal param------>");
    logdebug("size:%d", parmNormIstc.phead.psize);
    logdebug("crc:%d", parmNormIstc.phead.pcrc);
    // logdebug("sn:%.*s", (int)sizeof(parmNormIstc.sn), parmNormIstc.sn);
    logHexBufStr("parmNormPrint sn", parmNormIstc.sn, sizeof(parmNormIstc.sn));
    logdebug("addr:%d", parmNormIstc.addr485);
    logdebug("loglev:%d", parmNormIstc.loglev);
#if PARM_NORMAL_CHK_FLAG
    logdebug("chkparam:%d", parmNormIstc.chkparam);
#endif
    return 0;
}
int parmNormReset(int configLevel)
{
    switch (configLevel)
    {
    case PARAM_CFG_LEVEL_0:
    {
        memcpy(parmNormIstc.sn, LT_CMD_DEFAULT_SN, sizeof(parmNormIstc.sn));
        parmNormIstc.addr485 = LT_CMD_BOARDCAST_ADDR;
        parmNormIstc.loglev = 5;
    }
    case PARAM_CFG_LEVEL_1:
    {
    }
    case PARAM_CFG_LEVEL_2:
    {
    }
    case PARAM_CFG_LEVEL_3:
    {
    }
    case PARAM_CFG_LEVEL_4:
    {
    }
    case PARAM_CFG_LEVEL_5: // defualt
    {
#if PARM_NORMAL_CHK_FLAG
        parmNormIstc.chkparam = 88;
#endif
    }
    case PARAM_CFG_LEVEL_6:
    {
    }
    case PARAM_CFG_LEVEL_7:
    {
    }
    case PARAM_CFG_LEVEL_8:
    {
    }
    case PARAM_CFG_LEVEL_9:
    {
    }
    default:
    {
        break;
    }
    }
    parmCalcHeadInf(parmNormIstc);
}
void parmNormSave()
{
    parmCalcHeadInf(parmNormIstc);
    flashOptWrite(DEV_PARAM_FLASH_ADDR, &parmNormIstc, sizeof(parmNormIstc));
}
void parmNormRead()
{
    int tmpflag = 0;
    uint16_t tmpcrc;
    // for default log param to printf, new param make to default.
    parmNormReset(PARAM_CFG_LEVEL_0);
    // read form hard
    tmpflag = flashOptRead(DEV_PARAM_FLASH_ADDR, &parmNormIstc, sizeof(parmNormIstc));
    logdebug("---------read flash[%d]:%d,%d", tmpflag, parmNormIstc.phead.psize, (int)paramGetDataSize(parmNormIstc));
    if(tmpflag <= 0)
    {// for file
        parmNormReset(PARAM_CFG_LEVEL_0);
        tmpflag = 1;
    }
    else if(parmNormIstc.phead.psize == 0xffff)
    {
        // flash is erase
        logdebug("flash is have no data");
        parmNormReset(PARAM_CFG_LEVEL_0);
        tmpflag = 1;
    }
    else if(parmNormIstc.phead.psize <= paramGetDataSize(parmNormIstc))
    {
        // flash is old param, and new version add param. check flash param.
        // param is same size, check flash param.
        tmpcrc = parmCaclHeadCrc(parmNormIstc);
        if(parmNormIstc.phead.pcrc != tmpcrc)
        {
            logdebug("flash data crc error");
            parmNormReset(PARAM_CFG_LEVEL_0);
            tmpflag = 1;
        }
    }
    else if(parmNormIstc.phead.psize > paramGetDataSize(parmNormIstc))
    {
        // flash is new param, and old version del param. not check.
        logdebug("flash data is biger than my data, just use it.");
        flashOptRead(DEV_PARAM_FLASH_ADDR, &parmNormIstc, sizeof(parmNormIstc));
        tmpflag = 1;
    }
    if(tmpflag)
    {
        parmNormSave();
    }
}
//================================default normal param end=============================================

#if 0
#include <deviceNormalParam.h>
struct deviceParameterNormalInformOld
{
    struct
    {
        uint16_t psize;
        uint16_t pcrc;
    };
    uint32_t prmT1;
    uint16_t prmT2;
};
struct deviceParameterNormalInformNew
{
    struct
    {
        uint16_t psize;
        uint16_t pcrc;
    };
    uint32_t prmT1;
    uint16_t prmT2;
    float prmT3;
    uint8_t prmT4[10];
    uint32_t prmT5;
};
int main()
{
    struct deviceParameterNormalInformOld tmpparmOld;
    struct deviceParameterNormalInformNew tmpparmNew;
    char *tmpConstStr;
    // parmInfoNorStc *tmpNormalParam = parmNormIstc;
    parmNormReset(0);
    parmNormPrint();
    memcpypro(&tmpparmOld, sizeof(tmpparmOld), parmNormIstc, sizeof(parmInfoNorStc));
    memcpypro(&tmpparmNew, sizeof(tmpparmNew), parmNormIstc, sizeof(parmInfoNorStc));

    tmpparmOld.prmT1 = 11;
    tmpparmOld.prmT2 = 12;
    parmCalcHeadInf((paramHeadComStc *)&tmpparmOld, paramGetDataSize(tmpparmOld));
    parmNormCfg(&tmpparmOld, 0);
    parmNormPrint();

    tmpparmNew.prmT1 = 21;
    tmpparmNew.prmT2 = 22;
    tmpparmNew.prmT3 = 23;
    memcpy(tmpparmNew.prmT4, "24-", 4);
    tmpparmNew.prmT5 = 25;
    parmCalcHeadInf((paramHeadComStc *)&tmpparmNew, paramGetDataSize(tmpparmNew));
    parmNormCfg(&tmpparmNew, 0);
    parmNormPrint();

    parmInfoNorStc tmpnormread;
    memcpy(&tmpnormread, parmNormIstc, sizeof(parmInfoNorStc));
    logHexData("parm norm data:", (const uint8_t *)&tmpnormread, sizeof(parmInfoNorStc));
    paramNormCvrtVal(&tmpnormread);
    logHexData("parm cvrt data:", (const uint8_t *)&tmpnormread, sizeof(parmInfoNorStc));
}
#endif
